const { get } = require('../../utils/request.js')
const EventBus = require('../../utils/eventBus.js')

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    cursorSpacing: {
      type: Number,
      value: -50,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showErrorPwdTip: false,
    showPageContainer: true,
    passwordInputValue: '',
    focusInputPwd: false,
    pwdValue: ['', '', '', '']
  },

  lifetimes: {
    attached() {
      const ss = this.disablePageContainer.bind(this);
      EventBus.on('password.showPageContainer',ss)
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    disablePageContainer(isShow){
      this.setData({
        showPageContainer: isShow
      })
    },

    activeInputPassword() {
      this.setData({
        focusInputPwd: true
      })
    },

    inputPassword({ detail: { value } }) {

      const defaultValue = ['', '', '', '']
      value.split('').forEach((item, i) => {
        defaultValue[i] = item
      })
      this.setData({
        pwdValue: defaultValue
      })
      if (value.length == 4) {
        const pwd = this.data.pwdValue.join('')
        get('familyconfig/check/password', { password: pwd }, (msg) => {
          if (msg == '密码错误') {
            this.setData({
              showErrorPwdTip: true
            })
          } else {
            wx.hideKeyboard()
            this.cancelBind();
            this.triggerEvent('successCallback');
          }

        })
      } else {
        this.setData({
          showErrorPwdTip: false
        })
      }
    },

    cancelBind() {
      this.setData({
        pwdValue: ['', '', '', ''],
        passwordInputValue: '',
        show: false,
        showErrorPwdTip: false
      })
      wx.hideKeyboard()
    }

  }
})